home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / yk211src.lha / Yak_2.11_Src / Prefs / BGUI / Hotkey_window.c < prev    next >
C/C++ Source or Header  |  1995-11-16  |  24KB  |  561 lines

  1. /************************************************************************
  2. *************************  BGUI HOTKEY WINDOW  **************************
  3. ************************************************************************/
  4.  
  5. #include <exec/types.h>
  6. #include <intuition/intuition.h>
  7. #include <intuition/classes.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/intuitionbase.h>
  12. #include <libraries/bgui.h>
  13. #include <libraries/bgui_macros.h>
  14. #include <graphics/displayinfo.h>
  15. #include <graphics/gfxbase.h>
  16. #include <proto/exec.h>
  17. #include <proto/intuition.h>
  18. #include <proto/bgui.h>
  19. #include <proto/graphics.h>
  20. #include <proto/locale.h>
  21. #include <proto/utility.h>
  22. #include <proto/wb.h>
  23. #include <clib/alib_protos.h>
  24. #include <string.h>
  25.  
  26. #include "code.h"
  27. #include "yak.h"
  28. #include "version.h"
  29. #include "hotkey_types.h"
  30. #include "gui.h"
  31. #include "hotkey_window.h"
  32. #include "KeyDef_window.h"
  33. #include "Options_window.h"
  34. #include "Requesters.h"
  35.  
  36. #define CATCOMP_NUMBERS
  37. #include "yak_locale_strings.h"
  38.  
  39. #ifdef _DCC
  40. #  include <lists.h>
  41. #else
  42. #  include "sas_lists.h"
  43. #endif
  44.  
  45. /************************************************************************
  46. *****************************  REFERENCES  ******************************
  47. ************************************************************************/
  48.  
  49. IMPORT LONG (*HandleReturnID)(LONG ID);
  50. IMPORT struct MinList Actions1List;
  51.  
  52. extern void HookEntry(void);                    /* from amiga.lib */
  53.  
  54. IMPORT char *PrefsHelp;
  55.  
  56. /************************************************************************
  57. *****************************  PROTOTYPES  ******************************
  58. ************************************************************************/
  59.  
  60. static void InitHook(struct Hook *hook,ULONG (*hookfn)(),APTR data);
  61.  
  62. static SAVEDS ASM ULONG ExecListRsrcHandler(REG(a0) struct Hook *hook, REG(a2) void *obj, REG(a1) void *msg);
  63. static SAVEDS ASM ULONG ExecListDispHandler(REG(a0) struct Hook *hook, REG(a2) void *obj, REG(a1) void *msg);
  64.  
  65. static void UpdateHotKeyLV(struct List *list);
  66.  
  67. /************************************************************************
  68. *****************************  LOCAL DATA  ******************************
  69. ************************************************************************/
  70.  
  71. struct Window   *HotKeyWnd = NULL;
  72. Object                  *HotKeyWndObj = NULL;
  73. Object                  *HotKeyObjects[HotKey_CNT];
  74. struct IBox             HotKeyBox;
  75. BOOL                    HotKeyBoxValid = FALSE;
  76.  
  77. char                    *Actions1Array[NUM_HOTKEY_TYPES+1];
  78.  
  79. struct Hook             ExecListRsrcHook;
  80. struct Hook             ExecListDispHook;
  81.  
  82. /************************************************************************
  83. *****************************  INITHOOK()  ******************************
  84. *************************************************************************
  85. * Initialize the hook struct supplied with the hook function and
  86. * (optional) hook data ptr.
  87. *
  88. *************************************************************************/
  89.  
  90. static void InitHook(struct Hook *hook,ULONG (*hookfn)(),APTR data)
  91. {
  92. hook->h_Entry = (ULONG (*)()) HookEntry;
  93. hook->h_SubEntry = hookfn;
  94. hook->h_Data = data;
  95. }
  96.  
  97. /************************************************************************
  98. ************************  EXECLISTRSRCHANDLER()  ************************
  99. *************************************************************************
  100. * Hook called by a listview gadget for adding and deleting entries,
  101. * which are Exec nodes. Simply returns the entry ptr if MAKE, 0 if KILL.
  102. * Called from HookEntry().
  103. *
  104. *************************************************************************/
  105.  
  106. static SAVEDS ASM ULONG ExecListRsrcHandler(REG(a0) struct Hook *hook, REG(a2) void *obj, REG(a1) void *msg)
  107. {
  108. struct lvResource       *lvr;
  109.  
  110. lvr = (struct lvResource *) msg;
  111.  
  112. if (lvr->lvr_Command == LVRC_MAKE)
  113.         return((ULONG)lvr->lvr_Entry);
  114.  
  115. return(0);
  116. }
  117.  
  118. /************************************************************************
  119. ************************  EXECLISTDISPHANDLER()  ************************
  120. *************************************************************************
  121. * Hook called by a listview gadget for rendering. Simply returns a ptr
  122. * to the ln_Name of Exec Node struct that is the entry. The internal
  123. * BGUI renderer will then display this string. Called from HookEntry().
  124. *
  125. *************************************************************************/
  126.  
  127. static SAVEDS ASM ULONG ExecListDispHandler(REG(a0) struct Hook *hook, REG(a2) void *obj, REG(a1) void *msg)
  128. {
  129. struct lvRender *lvr;
  130.  
  131. lvr = (struct lvRender *) msg;
  132. return((ULONG)(((struct Node *) (lvr->lvr_Entry))->ln_Name));
  133. }
  134.  
  135. /************************************************************************
  136. ************************  CREATEHOTKEYWINDOW()  *************************
  137. *************************************************************************
  138. * Create the hotkey window object tree. Returns the window object ptr or
  139. * NULL if create failed. Gadgets are initialised with default values,
  140. * InitHotKeyGadgets must be called after window opened to reflect current
  141. * settings.
  142. *
  143. *************************************************************************/
  144.  
  145. APTR CreateHotKeyWindow(VOID)
  146. {
  147. Object  *obj;
  148. UWORD   i;
  149.  
  150. /* initialize array of ptrs to names of all the hotkey types */
  151. for(i=0;i<NUM_HOTKEY_TYPES;i++)
  152.         Actions1Array[i]  = getString(yhktypes[i].yhkt_nameID);
  153. Actions1Array[i] = NULL;
  154.  
  155. /* initialize the resource and display hooks for the hotkeys listview */
  156. InitHook(&ExecListRsrcHook,ExecListRsrcHandler,NULL);
  157. InitHook(&ExecListDispHook,ExecListDispHandler,NULL);
  158.  
  159. obj = WindowObject,
  160.         WINDOW_PubScreenName, PubScreenName,
  161.         WINDOW_Title,           getString(EDIT_HOTKEYS_STRING),
  162.         WINDOW_ScreenTitle,     getString(COPYRIGHT_STRING),
  163.         WINDOW_SmartRefresh, TRUE,
  164.         WINDOW_SizeBottom,      FALSE,                  /* put size gadget in right border */
  165.         WINDOW_SizeRight,       TRUE,
  166.         WINDOW_AutoAspect,      TRUE,                   /* automatic edges pixel width */
  167.         WINDOW_Position,        POS_CENTERMOUSE,
  168.         (HotKeyBoxValid ? WINDOW_Bounds : TAG_IGNORE), &HotKeyBox,
  169.         WINDOW_AppWindow,       TRUE,                   /* make it an appwindow */
  170.         WINDOW_RMBTrap,         TRUE,                   /* trap rmb - no menu */
  171.         WINDOW_CloseGadget,     FALSE,                  /* no windowclose gadget please */
  172.         (TextFont ? WINDOW_Font : TAG_IGNORE),  &Attr,  /* use font if specified */
  173.         WINDOW_HelpFile,        PrefsHelp,              /* online help system */
  174.         WINDOW_HelpNode,        "Hotkeys",
  175.         WINDOW_MasterGroup,
  176.                 VGroupObject, Spacing(4), HOffset(4), VOffset(4),
  177.                         StartMember,
  178.                                 HGroupObject, Spacing(2),
  179.                                         GROUP_EqualWidth, TRUE,
  180.                                         StartMember,
  181.                                                 LV_Actions = ListviewObject,
  182.                                                         LAB_Label,              getString(ACTIONS_STRING),
  183.                                                         LAB_Place,              PLACE_ABOVE,
  184.                                                         LAB_Highlight,  TRUE,
  185.                                                         GA_ID,                  GD_Actions,
  186.                                                         LISTV_EntryArray,Actions1Array,
  187.                                                         PGA_NewLook,    TRUE,
  188.                                                 EndObject,
  189.                                         EndMember,
  190.                                         VarSpace(DEFAULT_WEIGHT/20),
  191.                                         StartMember,
  192.                                                 VGroupObject, Spacing(2),
  193.                                                         StartMember,
  194.                                                                 LV_HotKeys = ListviewObject,
  195.                                                                         LAB_Label,              getString(HOTKEYS_STRING),
  196.                                                                         LAB_Place,              PLACE_ABOVE,
  197.                                                                         LAB_Highlight,  TRUE,
  198.                                                                         GA_ID,                  GD_Hotkeys,
  199.                                                                         LISTV_ResourceHook,&ExecListRsrcHook,
  200.                                                                         LISTV_DisplayHook,&ExecListDispHook,
  201.                                                                         PGA_NewLook,    TRUE,
  202.                                                                         LISTV_MinEntriesShown, 5,
  203.                                                                 EndObject, Weight(DEFAULT_WEIGHT*5),
  204.                                                         EndMember,
  205.                                                         StartMember,
  206.                                                                 STR_Name = StringObject,
  207.                                                                         STRINGA_MaxChars,512,
  208.                                                                         GA_ID,                  GD_Name,
  209.                                                                         RidgeFrame,
  210.                                                                 EndObject,
  211.                                                         EndMember,
  212.                                                         VarSpace(DEFAULT_WEIGHT/10),
  213.                                                         StartMember,
  214.                                                                 HGroupObject, Spacing(2), EqualWidth,
  215.                                                                         StartMember,
  216.                                                                                 BT_Add = Button(
  217.                                                                                         getString(ADD_STRING),
  218.                                                                                         GD_Add),
  219.                                                                         EndMember,
  220.                                                                         StartMember,
  221.                                                                                 BT_Delete = Button(
  222.                                                                                         getString(DELETE_STRING),
  223.                                                                                         GD_Delete),
  224.                                                                         EndMember,
  225.                                                                 EndObject,
  226.                                                         EndMember,
  227.                                                         VarSpace(DEFAULT_WEIGHT/10),
  228.                                                         StartMember,
  229.                                                                 HGroupObject, Spacing(2), EqualWidth,
  230.                                                                         StartMember,
  231.                                                                                 BT_Definition = Button(
  232.                                                                                         getString(DEFINITION_STRING),
  233.                                                                                         GD_Definition),
  234.                                                                         EndMember,
  235.                                                                         StartMember,
  236.                                                                                 BT_Options = Button(
  237.                                                                                         getString(OPTIONS_STRING),
  238.                                                                                         GD_Options),
  239.                                                                         EndMember,
  240.                                                                 EndObject,
  241.                                                         EndMember,
  242.                                                         VarSpace(DEFAULT_WEIGHT/10),
  243.                                                         StartMember,
  244.                                                                 HGroupObject, Spacing(2),
  245.                                                                         VarSpace(DEFAULT_WEIGHT),
  246.                                                                         StartMember,
  247.                                                                                 CH_State = CheckBoxObject,
  248.                                                                                         LAB_Label,              getString(STATE_ACTIVE_STRING),
  249.                                                                                         LAB_Place,              PLACE_LEFT,
  250.                                                                                         GA_ID,                  GD_State,
  251.                                                                                         ButtonFrame,
  252.                                                                                         FRM_Flags,              FRF_EDGES_ONLY,
  253.                                                                                 EndObject, FixMinWidth,
  254.                                                                         EndMember,
  255.                                                                 EndObject,
  256.                                                         EndMember,
  257.                                                 EndObject,
  258.                                         EndMember,
  259.                                 EndObject, Weight(DEFAULT_WEIGHT*10),
  260.                         EndMember,
  261.                         VarSpace(DEFAULT_WEIGHT/10),
  262.                         StartMember,
  263.                                 HorizSeparator,
  264.                         EndMember,
  265.                         StartMember,
  266.                                 HGroupObject, Spacing(2),
  267.                                         VarSpace(DEFAULT_WEIGHT),
  268.                                         StartMember,
  269.                                                 BT_Return = Button(getString(OK_STRING),GD_Return),
  270.                                         EndMember,
  271.                                         VarSpace(DEFAULT_WEIGHT),
  272.                                 EndObject,
  273.                         EndMember,
  274.                 EndObject,
  275.         EndObject;
  276.  
  277. return(obj);
  278. }
  279.  
  280. /************************************************************************
  281. *************************  CLOSEHOTKEYWINDOW()  *************************
  282. *************************************************************************
  283. * Close the hotkey window, disposing of the window object.
  284. *
  285. *************************************************************************/
  286.  
  287. void CloseHotKeyWindow(void)
  288. {
  289. if (HotKeyWnd)
  290.         {
  291.         GetAttr(WINDOW_Bounds,HotKeyWndObj,(ULONG *) &HotKeyBox);
  292.         HotKeyBoxValid = TRUE;
  293.         }
  294.  
  295. DisposeObject(HotKeyWndObj);
  296. HotKeyWndObj = NULL;
  297. HotKeyWnd = NULL;
  298. }
  299.  
  300. /***************************************************************************
  301.  *      HOTKEY SPECIFIC DATA/ROUTINES
  302.  *      perhaps move to another file?
  303.  ***************************************************************************/
  304.  
  305. /* prototypes */
  306. static struct Node *GetNode(struct List *lh, UWORD n);
  307. static UWORD GetNodeNum(struct List *lh, struct Node *node);
  308.  
  309. /* external data */
  310. YakHotKey       *curhk=NULL;
  311. UWORD           curtype=0;
  312.  
  313. /* get number of node in list - node MUST be there! */
  314. static UWORD
  315. GetNodeNum(struct List *lh, struct Node *node)
  316. {
  317.         struct Node *ln;
  318.         UWORD i;
  319.  
  320.         for (i = 0, ln = GetHead(lh); ln != node; ln = GetSucc(ln), i++)
  321.                 ;
  322.         return i;
  323. }
  324.  
  325. /* get nth node in list - list MUST have >= n nodes!!! */
  326. static struct Node *
  327. GetNode(struct List *lh, UWORD n)
  328. {
  329.         struct Node *ln;
  330.  
  331.         for (ln = GetHead(lh); n--; ln = GetSucc(ln))
  332.                 ;
  333.         return ln;
  334. }
  335.  
  336. /************************************************************************
  337. **************************  UPDATEHOTKEYLV()  ***************************
  338. *************************************************************************
  339. * Make the hotkeys listview display the hotkeys in the supplied list
  340. * (which may be empty).
  341. *
  342. *************************************************************************/
  343.  
  344. static void UpdateHotKeyLV(struct List *list)
  345. {
  346. struct Node     *n;
  347.  
  348. LockList(LV_HotKeys);
  349. ClearList(HotKeyWnd, LV_HotKeys);
  350.  
  351. if (list)
  352.         {
  353.         for (n=GetHead(list); n; n=GetSucc(n))
  354.                 AddEntry(HotKeyWnd, LV_HotKeys, n, LVAP_TAIL);
  355.         }
  356.  
  357. UnlockList(HotKeyWnd,LV_HotKeys);
  358. }
  359.  
  360. /************************************************************************
  361. ************************  INITHOTKEYGADGETS()  **************************
  362. *************************************************************************
  363. * Initialize the hotkey window gadgets to reflect current settings.
  364. * Supply hotkeylv TRUE if you want the hotkeys listview updated as
  365. * well.
  366. *************************************************************************/
  367.  
  368. static void InitHotKeyGadgets(BOOL hotkeylv)
  369. {
  370. UWORD   n;
  371.  
  372. if (hotkeylv)
  373.         UpdateHotKeyLV(keylist(curtype));
  374.  
  375. InitWindowGadget(GDX_Actions, LISTV_Select, curtype, HOTKEY_WINDOW);
  376. InitWindowGadget(GDX_Delete, GA_Disabled, !curhk, HOTKEY_WINDOW);
  377. InitWindowGadget(GDX_Definition, GA_Disabled, !curhk, HOTKEY_WINDOW);
  378.  
  379. if (curhk)              /* something selected */
  380.         {
  381.         n = GetNodeNum(keylist(curtype), (struct Node *)curhk);
  382.  
  383.         InitWindowGadget(GDX_Name, GA_Disabled, FALSE, HOTKEY_WINDOW);
  384.         InitWindowGadget(GDX_Name, STRINGA_TextVal, (LONG)curhk->yhk_Name, HOTKEY_WINDOW);
  385.         InitWindowGadget(GDX_State, GA_Disabled, FALSE, HOTKEY_WINDOW);
  386.         InitWindowGadget(GDX_State, GA_Selected, (LONG)curhk->yhk_State, HOTKEY_WINDOW);
  387.         InitWindowGadget(GDX_Actions, LISTV_Select, curtype, HOTKEY_WINDOW);
  388.         InitWindowGadget(GDX_Hotkeys, LISTV_Select, n, HOTKEY_WINDOW);
  389.  
  390.         if (YHK_Takes_Opt(curhk))
  391.                 InitWindowGadget(GDX_Options, GA_Disabled, FALSE, HOTKEY_WINDOW);
  392.         else
  393.                 InitWindowGadget(GDX_Options, GA_Disabled, TRUE,  HOTKEY_WINDOW);
  394.         }
  395. else    /* no current hotkey */
  396.         {
  397.         InitWindowGadget(GDX_Name, STRINGA_TextVal, (LONG)"", HOTKEY_WINDOW);
  398.         InitWindowGadget(GDX_Name, GA_Disabled, TRUE, HOTKEY_WINDOW);
  399.         InitWindowGadget(GDX_State, GA_Selected, FALSE, HOTKEY_WINDOW);
  400.         InitWindowGadget(GDX_State, GA_Disabled, TRUE, HOTKEY_WINDOW);
  401.         InitWindowGadget(GDX_Options, GA_Disabled, TRUE, HOTKEY_WINDOW);
  402.         }
  403. }
  404.  
  405. /************************************************************************
  406. ***********************  HANDLEHOTKEYRETURNID()  ************************
  407. *************************************************************************
  408. * Handle msgs arriving at hotkey window - supply the ID returned by
  409. * BGUI. Returns either RET_OKAY or RET_QUIT.
  410. *
  411. *************************************************************************/
  412.  
  413. LONG HandleHotKeyReturnID(LONG id)
  414. {
  415. LONG    ret;
  416. ULONG   code;
  417. APTR    err;
  418.  
  419. ret = RET_OKAY;
  420.  
  421. switch(id)
  422.         {
  423.         case GD_Actions:
  424.                 GetAttr(LISTV_LastClickedNum,LV_Actions,&code);
  425.                 if (code != curtype)
  426.                         {
  427.                         curtype = code;
  428.                         curhk = (YakHotKey *)GetHead(keylist(curtype));
  429.                         InitHotKeyGadgets(TRUE);
  430.                         }
  431.                 break;
  432.  
  433.         case GD_Hotkeys:
  434.                 GetAttr(LISTV_LastClicked,LV_HotKeys,&code);
  435.                 curhk = (YakHotKey *)code;
  436.                 InitHotKeyGadgets(FALSE);
  437.                 break;
  438.  
  439.         case GD_State:
  440.                 GetAttr(GA_Selected,CH_State,&code);
  441.                 if (curhk)
  442.                         curhk->yhk_State = (code ? TRUE : FALSE);
  443.                 InitHotKeyGadgets(FALSE);
  444.                 break;
  445.  
  446.         case GD_Add:
  447.                 if (curhk=NewYakHotKey(curtype))
  448.                         {
  449.                         DefaultOptions(curhk);
  450.                         InitHotKeyGadgets(TRUE);
  451.                         ActivateGadget((struct Gadget *)STR_Name, HotKeyWnd, NULL);
  452.                         }
  453.                 break;
  454.  
  455.         case GD_Delete:
  456.                 if (curhk)
  457.                         {
  458.                         DeleteYakHotKey(curhk);
  459.                         curhk = NULL;
  460.                         InitHotKeyGadgets(TRUE);
  461.                         }
  462.                 break;
  463.  
  464.         case GD_Name:
  465.                 GetAttr(STRINGA_TextVal,STR_Name,&code);
  466.                 if (!ModifyYHKName(curhk, (char *)code))
  467.                         ActivateGadget((struct Gadget *)STR_Name, HotKeyWnd, NULL);
  468.                 InitHotKeyGadgets(TRUE);
  469.                 break;
  470.  
  471.         case GD_Definition:
  472.                 if (curhk)
  473.                         {
  474.                         if (!SwitchToKeyDefWindow(HOTKEY_WINDOW,curhk,curhk->yhk_Name))
  475.                                 {
  476.                                 PostError(getString(Couldnt_open_other_window_ERR));
  477.                                 ret= RET_QUIT;
  478.                                 }
  479.                         }
  480.                 break;
  481.  
  482.         case GD_Options:
  483.                 if (curhk)
  484.                         {
  485.                         if (!SwitchToOptionsWindow())
  486.                                 {
  487.                                 PostError(getString(Couldnt_open_other_window_ERR));
  488.                                 ret = RET_QUIT;
  489.                                 }
  490.                         }
  491.                 break;
  492.  
  493.         case GD_Return:
  494.                 err = NULL;
  495.  
  496.                 for (curtype=0; (curtype < NUM_HOTKEY_TYPES) && !err; curtype++)
  497.                         {
  498.                         for (curhk=(YakHotKey *)GetHead(keylist(curtype));
  499.                                 curhk && !(err=ControlYakHotKey(curhk));
  500.                                         curhk=(YakHotKey *)GetSucc(curhk))
  501.                                                 /**/;
  502.                         }
  503.  
  504.                 if (!err)
  505.                         {
  506.                         curtype = 0;
  507.                         CloseHotKeyWindow();
  508.                         if (!ShowWindowID(ROOT_WINDOW))
  509.                                 ret = RET_QUIT;
  510.                         }
  511.                 else
  512.                         {
  513.                         curtype--;
  514.                         InitHotKeyGadgets(TRUE);
  515.                         PostError(getString(err));
  516.                         }
  517.                 break;
  518.  
  519.         default:
  520.                 break;
  521.         }
  522.  
  523. return(ret);
  524. }
  525.  
  526. /************************************************************************
  527. *************************  SHOWHOTKEYWINDOW()  **************************
  528. *************************************************************************
  529. * Create and open the hotkey window and initialize the gadgets to
  530. * reflect current settings. Sets up HandleReturnID function, curwin,
  531. * curwinobj and curwinID. Returns TRUE or FALSE to indicate success.
  532. *
  533. *************************************************************************/
  534.  
  535. BOOL ShowHotKeyWindow(void)
  536. {
  537. if (HotKeyWndObj = (Object *)CreateHotKeyWindow())
  538.         {
  539.         if (HotKeyWnd = WindowOpen(HotKeyWndObj))
  540.                 {
  541.                 if (curwinID == ROOT_WINDOW)
  542.                         curhk = (YakHotKey *)GetNode(keylist(curtype), 0);
  543.                 InitHotKeyGadgets(TRUE);
  544.                 HandleReturnID = HandleHotKeyReturnID;
  545.                 curwin = HotKeyWnd;
  546.                 curwinobj = HotKeyWndObj;
  547.                 curwinID = HOTKEY_WINDOW;
  548.                 return(TRUE);
  549.                 }
  550.         }
  551.  
  552. CloseHotKeyWindow();
  553. curwin = NULL;
  554. curwinobj = NULL;
  555. wndsigflag = 0L;
  556. appwinsigflag = 0L;
  557. return(FALSE);
  558. }
  559.  
  560.